package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.DriverEditResult;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.GuestsDriverMapper;
import com.yh.homestay.dao.GuestsDriverLikeMapper;
import com.yh.homestay.pojo.GuestsDriver;
import com.yh.homestay.pojo.GuestsDriverExample;
import com.yh.homestay.service.GuestsDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GuestsDriverServiceImpl implements GuestsDriverService {
    @Autowired
    private GuestsDriverMapper driverMapper;

    @Autowired
    private GuestsDriverLikeMapper selectDriverMapper;

    @Override
    public HomeStayResult selectall(Integer page, Integer limit, String driverName) {
        Map map = new HashMap();
        map.put("driverName",driverName);
        PageHelper.startPage(page,limit);
        List<GuestsDriver> guestsDrivers = selectDriverMapper.findByName1(map);
        PageInfo pageInfo = new PageInfo(guestsDrivers);
        HomeStayResult res=new HomeStayResult(0," ", (int) pageInfo.getTotal(),pageInfo.getList());
        return res;
    }

    @Override
    public HomeStayResult addDriver(GuestsDriver guestsDriver) {
        int insert = driverMapper.insert(guestsDriver);
        if (insert > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }

    @Override
    public HomeStayResult delete(Integer id) {
        GuestsDriver guestsDriver = new GuestsDriver();
        guestsDriver.setDriverId(id);
        int delete = driverMapper.delete(guestsDriver);
        if (delete > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }

    @Override
    public HomeStayResult update(Integer id, String driverName, String driverPhone, String driverDrivingCreateTime, String driverDrivingExpiredTime, String driverDrivingPic, String driverIdcardA, String driverIdcardB, String driverDrivingType, Integer driverStatus) {
        GuestsDriver guestsDriver = new GuestsDriver();
        guestsDriver.setDriverId(id);
        guestsDriver.setDriverName(driverName);
        guestsDriver.setDriverPhone(driverPhone);
        guestsDriver.setDriverDrivingCreateTime(driverDrivingCreateTime);
        guestsDriver.setDriverDrivingExpiredTime(driverDrivingExpiredTime);
        guestsDriver.setDriverDrivingPic(driverDrivingPic);
        guestsDriver.setDriverIdcardA(driverIdcardA);
        guestsDriver.setDriverIdcardB(driverIdcardB);
        guestsDriver.setDriverDrivingType(driverDrivingType);
        guestsDriver.setDriverStatus(driverStatus);
        GuestsDriverExample example = new GuestsDriverExample();
        GuestsDriverExample.Criteria criteria = example.createCriteria();
        criteria.andDriverIdEqualTo(guestsDriver.getDriverId());
        int i = driverMapper.updateByExample(guestsDriver, example);
        if(i>0){
            return new HomeStayResult(200,"ok",0,null);
        }else {
            return new HomeStayResult(500,"error",0,null);
        }

    }

    @Override
    public DriverEditResult selectOne(Integer id) {
        GuestsDriverExample example = new GuestsDriverExample();
        GuestsDriverExample.Criteria criteria = example.createCriteria();
        criteria.andDriverIdEqualTo(id);
        List<GuestsDriver> guestsDrivers = driverMapper.selectByExample(example);
        if(guestsDrivers!=null&&guestsDrivers.size()>0){
            return new DriverEditResult(200,"ok",guestsDrivers.get(0));
        }else{
            return new DriverEditResult(500,"err",null);
        }
    }

    @Override
    public HomeStayResult deleteAll(List<Integer> ids) {
        GuestsDriverExample example = new GuestsDriverExample();
        GuestsDriverExample.Criteria criteria = example.createCriteria();
        criteria.andDriverIdIn(ids);
        int delete = driverMapper.deleteByExample(example);
        if (delete > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }
}
